//
//  SpaceViewContent.m
//  cutter
//
//  Created by mac on 15/12/21.
//  Copyright © 2015年 e360. All rights reserved.
//
#import "SpaceViewController.h"
#import "SpaceCustom.h"
#import "TMGlobal.h"
#import "TMLoader.h"
#import "TMRequest.h"
#import "TMJSON.h"
#import "TMUtils.h"
#import "IndexHomeCtrl.h"
#import "SpaceEditController.h"
#import "UserCenterPick.h"

#pragma mark -
#pragma mark SpaceViewControllerCell
@implementation SpaceViewControllerCell
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self.layer setMasksToBounds:YES];
        [self.layer setCornerRadius:4];
    }
    return self;
}
@end


#pragma mark -
#pragma mark SpaceViewController
@implementation SpaceViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    tips = [[SpaceClassifyTips alloc] initWithFrame:CGRectZero];
    self.collectView.hidden = YES;
    //
    [self.collectionView.headerView addTarget:self action:@selector(headerRefresh:) forControlEvents:UIControlEventValueChanged];
    [self.collectionView.headerView setTitle:@"正在刷新" forState:TMRefreshStateRefreshing];
    [self.collectionView.headerView setTitle:@"释放更新" forState:TMRefreshStatePulling];
    [self.collectionView.headerView setTitle:@"下拉刷新" forState:TMRefreshStateNormal];
    //
    [self.collectionView.footerView addTarget:self action:@selector(footerRefresh:) forControlEvents:UIControlEventValueChanged];
    [self.collectionView.footerView setTitle:@"正在刷新" forState:TMRefreshStateRefreshing];
    [self.collectionView.footerView setTitle:@"释放更新" forState:TMRefreshStatePulling];
    [self.collectionView.footerView setTitle:@"上拉翻页" forState:TMRefreshStateNormal];
    //
    [self.collectView addTarget:self action:@selector(collectTouch:) forControlEvents:UIControlEventTouchUpInside];
    [self.classifyView addTarget:self action:@selector(ctrlTouch:) forControlEvents:UIControlEventTouchUpInside];
    [self.screenView addTarget:self action:@selector(ctrlTouch:) forControlEvents:UIControlEventTouchUpInside];
    //
    [self.mixedView addTarget:self action:@selector(sortTouch:) forControlEvents:UIControlEventTouchUpInside];
    [self.salesView addTarget:self action:@selector(sortTouch:) forControlEvents:UIControlEventTouchUpInside];
    [self.priceView addTarget:self action:@selector(sortTouch:) forControlEvents:UIControlEventTouchUpInside];
    //
    self.menuView.currentTitle = @"空间";
    [self.menuView.clearbtn addTarget:self action:@selector(clearTouch:) forControlEvents:UIControlEventTouchUpInside];
    //
    [self hiddenLeft:NO];
    [self addGestureRecognizer];
    //
    if(self.type==0){
        [self headerRefresh:nil];
    }
}
//
-(void)clearTouch:(UIButton *)sender{
    [self.condition setValue:nil forKey:@"keyword"];
    [self headerRefresh:nil];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    if([self.menuView.textField.text isEqualToString:@""]){
        [self.condition setValue:nil forKey:@"keyword"];
    }else{
        [self.condition setValue:textField.text forKey:@"keyword"];
    }
    [self headerRefresh:nil];
    [textField resignFirstResponder];
    return NO;
}

- (void)headerRefresh:(TMRefresh*)sender{
    if (sender == nil) {
        sender = self.collectionView.headerView;
        [sender beginRefreshing];
    }
    //
    __weak SpaceViewController *this = self;
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [this.condition setValue:@(1) forKey:@"page"];
        [this setData:[[SpaceData shareInstance] getSpaceProducts:this.condition]];
        dispatch_async(dispatch_get_main_queue(), ^{
            [sender endRefreshing];
            [this.nullView setHidden:[this.data count]>0];
            [this.collectionView.footerView setFinish:NO];
            //
            [this.collectionView reloadData];
        });
    });
}

- (void)footerRefresh:(TMRefresh*)sender{
    __weak SpaceViewController *this = self;
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSInteger p = [[self.condition objectForKey:@"page"] intValue] + 1;
        [this.condition setValue:@(p) forKey:@"page"];
        //
        NSArray *app = [[SpaceData shareInstance] getSpaceProducts:this.condition];
        [this.data addObjectsFromArray:app];
        dispatch_async(dispatch_get_main_queue(), ^{
            [sender setFinish:[app count]==0];
            [sender endRefreshing];
            //
            [this.collectionView reloadData];
        });
    });
}
- (void)hiddenLeft:(BOOL)animation{
    [self.leftLayout setConstant:-250];
    [self.view setNeedsUpdateConstraints];
    if (animation) {
        [UIView beginAnimations:nil context:nil];
        [self.view layoutIfNeeded];
        [UIView commitAnimations];
    }
}
- (void)showLeft:(BOOL)animation{
    [self.leftLayout setConstant:0];
    [self.view setNeedsUpdateConstraints];
    if (animation) {
        [UIView beginAnimations:nil context:nil];
        [self.view layoutIfNeeded];
        [UIView commitAnimations];
    }
}
- (IBAction)exitTouch:(id)sender {
    IndexHomeCtrl *tabbar = (id)self.splitViewController;
    if ([tabbar isKindOfClass:IndexHomeCtrl.class]) {
        [tabbar showMenu];
    }
}
- (void)ctrlTouch:(UIButton*)sender{
    if (sender.selected) {
        [sender setSelected:NO];
        if (self.classifyView.selected == NO && self.screenView.selected == NO) {
            [self hiddenLeft:YES];
        }
    }else{
        [self.classifyView setSelected:NO];
        [self.screenView setSelected:NO];
        [sender setSelected:YES];
        [self showLeft:YES];
    }
    //
    for (uint i = 0; i < self.childViewControllers.count; i++) {
        UIViewController *ctrl  = [self.childViewControllers objectAtIndex:i];
        if (self.classifyView.selected != i || self.screenView.selected == i) {
            [ctrl.view.superview setHidden:NO];
        }else{
            [ctrl.view.superview setHidden:YES];
        }
    }
}
- (void)collectTouch:(UIButton*)sender{
}
- (void)sortTouch:(UIButton*)sender{
    //|=%7C
    if (sender==self.priceView) {
        //
        [self.mixedView setSelected:NO];
        [self.salesView setSelected:NO];
        
        if (self.priceView.selected == YES) {
            [self.priceView setSort:!self.priceView.sort];
        }else{
            [self.priceView setSelected:YES];
            [self.priceView setSort:NO];
        }
        [self.condition setValue:@"" forKey:@"id"];
        if (self.priceView.sort) {
            [self.condition setValue:@"sort=spacearea&sorttype=desc" forKey:@"spacearea"];
        }else{
            [self.condition setValue:@"sort=spacearea&sorttype=asc" forKey:@"spacearea"];
        }
    }else{
        [self.priceView setSelected:NO];
        [self.mixedView setSelected:NO];
        [self.salesView setSelected:NO];
        [sender setSelected:YES];
        //
        [self.condition setValue:@"" forKey:@"spacearea"];
        if (sender == self.salesView) {
            [self.condition setValue:@"sort=id&sorttype=desc" forKey:@"id"];
        }else{
            [self.condition setValue:@"" forKey:@"sort"];
        }
    }
    [self headerRefresh:nil];
}

- (NSMutableArray *)data{
    if (_data == nil || (id)_data==[NSNull null]) {
        [self setData:[NSMutableArray array]];
    }
    return _data;
}
- (NSMutableDictionary *)condition{
    if (_condition == nil) {
        [self setCondition:[NSMutableDictionary dictionary]];
    }
    [_condition setValue:@(50) forKey:@"limit"];
    return _condition;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [self.data count];
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    SpaceViewControllerCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
    if([self.data count]>indexPath.item){
        id item = [self.data objectAtIndex:indexPath.item];
//        SpaceViewControllerCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
        [AppData loadImage:[item objectForKey:@"pic"] view:cell.imageView];
        [cell.priceView setText:[AppData areaToString:[item objectForKey:@"spacearea"] lable:nil]];
        [cell.titieView setText:[AppData objToString:[item objectForKey:@"suitname"] lable:nil]];
        cell.infoView.hidden = YES;
    }
     return cell;
}

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(nullable id)sender{
    //顶部导航
    if ([TMGlobal valueForKey:@"client"]) {
        if ([identifier isEqualToString:@"favorite"]) {
            [TMGlobal setValue:@(3) forKey:@"clientTag"];
            [TMGlobal setValue:@(8) forKey:@"homeIndex"];
            return NO;
        }
        if ([identifier isEqualToString:@"cart"]) {
            [TMGlobal setValue:@(4) forKey:@"clientTag"];
            [TMGlobal setValue:@(8) forKey:@"homeIndex"];
            return NO;
        }
        if ([identifier isEqualToString:@"client"]) {
            [TMGlobal setValue:nil forKey:@"clientTag"];
            [TMGlobal setValue:@(8) forKey:@"homeIndex"];
            return NO;
        }
    }
    return YES;
}
//
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(nullable id)sender{
    //顶部导航
    __weak SpaceViewController *this = self;
    if ([segue.destinationViewController isKindOfClass:UINavigationController.class]) {
        UINavigationController *ctrl = segue.destinationViewController;
        if ([[ctrl.viewControllers firstObject] isKindOfClass:UserCenterSelect.class]) {
            UserCenterSelect *root = [ctrl.viewControllers firstObject];
            [root setOnSelected:^(id target, id result){
                if(result){
                    if([segue.identifier isEqualToString:@"favorite"]){
                        [TMGlobal setValue:@(3) forKey:@"clientTag"];
                        [TMGlobal setValue:@(8) forKey:@"homeIndex"];
                    }
                    if([segue.identifier isEqualToString:@"cart"]){
                        [TMGlobal setValue:@(4) forKey:@"clientTag"];
                        [TMGlobal setValue:@(8) forKey:@"homeIndex"];
                    }
                }
                [this dismissViewControllerAnimated:YES completion:nil];
            }];
        }
    }
    //
    if ([sender isKindOfClass:UICollectionViewCell.class]) {
        NSIndexPath *indexPath = [self.collectionView indexPathForCell:sender];
        if (indexPath) {
            SpaceEditController *detail = segue.destinationViewController;
            if ([detail isKindOfClass:SpaceEditController.class]) {
                detail.classids= [[self.condition objectForKey:@"classids"] integerValue];
                detail.titleName =[[self.data objectAtIndex:indexPath.row] objectForKey:@"suitname"];
                [detail setSource:[self.data objectAtIndex:indexPath.row]];
            }
            [detail setOnSelected0:self.onSelected];
            [detail setType:self.type];
        }
        return;
    }
    
    
    

    
    
    //
//    __weak SpaceViewController *this = self;
    __weak UINavigationController *root = segue.destinationViewController;
    if ([segue.identifier isEqualToString:@"SpaceClassify"]) {
        __weak SpaceClassify *classify = [root.viewControllers firstObject];
        
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            [classify setData:[[SpaceData shareInstance] getSpaceClassify]];
            dispatch_async(dispatch_get_main_queue(), ^{
                [classify setTitle:[classify.data objectForKey:@"title"]];
                [classify.tableView reloadData];
            });
        });
        [classify setOnSelected:^(id sender, id result){
            texts = [result objectForKey:@"classname"];
            if ([sender isKindOfClass:NSIndexPath.class]) {
                if ([result objectForKey:@"items"]) {
                   
                    SpaceClassify *next = [this.storyboard instantiateViewControllerWithIdentifier:@"SpaceClassify"];
                    [root pushViewController:next animated:YES];
                    [next setOnSelected:classify.onSelected];
                    [next setData:result];
                }else{
                    [this.condition setValue:[result objectForKey:@"id"] forKey:@"classids"];
                    [this headerRefresh:nil];
                }
            }
        }];
        if(self.type ==1 && self.classids>0){
            NSInteger a=0;
            for(NSInteger i=0;i<[[classify.data objectForKey:@"child"] count];i++){
                id item =[[classify.data objectForKey:@"child"] objectAtIndex:i];
                if([[item objectForKey:@"id"] intValue] ==self.classids){
                    a=i;
                }
            }
            NSIndexPath *indexPath=[NSIndexPath indexPathForItem:a inSection:0];
            [classify.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionTop];
            [this.condition setValue:[NSNumber numberWithInteger:self.classids] forKey:@"classids"];
            [this headerRefresh:nil];
        }
        
        
    }
    
    
    //    __weak ProductClassifyCtrl *classify = [root.viewControllers firstObject];
    //    dispatch_async(dispatch_get_global_queue(0, 0), ^{
    //        [classify setData:[AppData productClassify]];
    //        [classify setTitle:[classify.data objectForKey:@"name"]];
    //        dispatch_async(dispatch_get_main_queue(), ^{
    //            [classify.tableView reloadData];
    //        });
    //    });

    
    
    //筛选
    if ([segue.identifier isEqualToString:@"SpaceScreen"]) {
        __weak SpaceClassify *screen = [root.viewControllers firstObject];
        __block SpaceViewController *this = self;
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            [screen setData:[[SpaceData shareInstance] getSpaceScreen:0]];
            dispatch_async(dispatch_get_main_queue(), ^{
                [screen setTitle:[screen.data objectForKey:@"title"]];
                [screen.tableView reloadData];
            });
        });
        [screen setOnSelected:^(id sender, id result){
            
//            SpaceClassifyTips *tips = [[SpaceClassifyTips alloc] initWithFrame:CGRectZero];
            CGRect rect = [root.view convertRect:root.view.bounds toView:this.view];
            [tips showFromRect:CGRectOffset(rect, rect.size.width, 0) atView:this.view];
            
            if([[result objectForKey:@"child"] isKindOfClass:NSArray.class]){
                [tips setTips:screen.tips];
                [tips setData:result];
                [tips reloadTips];
            }else{
                for(id item in tips.scrollView.subviews){
                    [item removeFromSuperview];
                }
                [this creatSize:tips.scrollView];
                [tips setData:this.sizeDic];
            }
            [screen setOnclose:^(id sender, id result)
            {
                
                NSLog(@"%@",result);
                if([[sender objectForKey:@"1000"] count]==0){
                    
                    [this.condition setValue:nil forKey:@"height"];
                    [this.condition setValue:nil forKey:@"width"];
                    [this.condition setValue:nil forKey:@"length"];
                    self.sizeDic = nil;
                }else{
                    //[tips reloadTips];
                }
                if([result intValue]!=999){
                     [tips reloadTips];
                }
               
                NSMutableString *str = [[NSMutableString alloc] initWithString:[NSString stringWithFormat:@"%@",[self.condition objectForKey:@"classids"]]];
                NSString *_str = [self getstr:str temp:result];
                
                [this.condition setValue:_str forKey:@"classids"];
                if(_str.length==0){
                    [this.condition setValue:nil forKey:@"classids"];
                }
                [this headerRefresh:nil];
            }];
            //
            [tips setOnSelected:^(id sender, id result){
                
                if([[[this.sizeDic objectForKey:@"1000"] objectForKey:@"999" ] objectForKey:@"classname" ]){
                    [screen.tips setValue:[NSMutableDictionary dictionaryWithDictionary:[this.sizeDic objectForKey:@"1000"]] forKey:@"1000"];
                    [this refreshUI];
                    [screen setTips:screen.tips];
                    [screen reloadTips];
                }
                //
                if (result) {
                    [screen setTips:result];
                    [screen reloadTips];
                }else{
                    [screen.tableView reloadData];
                    //
                    NSArray *keys = [NSArray arrayWithObjects:@"classids",@"length",@"width",@"height", nil];
                    
                    for (id key in keys) {
                        [this.condition setValue:nil forKey:key];
                    }
                    for (id group in screen.tips.allKeys) {
                        NSDictionary *val = [screen.tips objectForKey:group];
                        if([group intValue]==1000){
                            [this.condition setValue:[[val objectForKey:@"999"] objectForKey:@"height"] forKey:@"height"];
                            [this.condition setValue:[[val objectForKey:@"999"] objectForKey:@"width"] forKey:@"width"];
                            [this.condition setValue:[[val objectForKey:@"999"] objectForKey:@"length"] forKey:@"length"];
                        }
                        if (val&&[group intValue]!=1000) {
                            
                            NSString *str=@"";
                            for(id item in val.allKeys){
                                NSLog(@"item   === %@",item);
                                str =[str stringByAppendingFormat:@"%@,",item];
                                [this.condition setValue:str forKey:@"classids"];
                            }
                        }
                    }
                    
                    
                    NSLog(@"this.condition   == %@",this.condition);

                    [this headerRefresh:nil];
                }
            }];
        }];
        
        
    }
}

-(NSString*)getstr:(NSMutableString *)str temp:(id)temp{
    NSRange range = [str rangeOfString:[NSString stringWithFormat:@"%@",temp]];
    if(range.location<str.length){
        [str deleteCharactersInRange:range];
        NSArray *listItems = [str componentsSeparatedByString:@","];
        NSMutableArray *arr = [[NSMutableArray alloc]init];
        for(id item in listItems){
            if(![item isEqualToString:@""]){
                [arr addObject:item];
                
            }
        }
        if([arr count]==1){
            return [NSString stringWithFormat:@"%@",[arr objectAtIndex:0]];
        }
        return [arr componentsJoinedByString:@","];
    }
    return nil;
    
}
-(void)creatSize:(id)parent{
    lengthEnabledBtn=[[UIButton alloc]initWithFrame:CGRectMake(20, 20, 24, 24)];
    [parent addSubview:lengthEnabledBtn];
    [lengthEnabledBtn setImage:[UIImage imageNamed:@"btn_Radio_nor.png"]  forState:UIControlStateNormal];
    [lengthEnabledBtn setImage:[UIImage imageNamed:@"btn_Radio_sel.png"]   forState:UIControlStateSelected];
    [lengthEnabledBtn addTarget:self action:@selector(clicklengthEnabled:) forControlEvents:UIControlEventTouchUpInside];
    
    lengthTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(54, 23, 50, 18)];
    [parent addSubview:lengthTitleLabel];
    lengthTitleLabel.text=@"进深";
    lengthTitleLabel.font =[UIFont fontWithName:@"" size:14];
    lengthTitleLabel.textColor = [UIColor colorWithHex:0x4a4a4a];;
    lengthTitleLabel.textAlignment=1;
    
    lengthLabel = [[UILabel alloc]initWithFrame:CGRectMake(102, 23, 80, 18)];
    [parent addSubview:lengthLabel];
    lengthLabel.text=@"不限";
    lengthLabel.font =[UIFont fontWithName:@"" size:14];
    lengthLabel.textColor = [UIColor colorWithHex:0x4a4a4a];;
    lengthLabel.textAlignment=1;
    //
    lengthSlider = [[UISlider alloc]initWithFrame:CGRectMake(20, 60, 214, 6)];
    lengthSlider.maximumValue = 12;
    lengthSlider.minimumValue = 0.6;
    lengthSlider.value = 4.2;
    lengthSlider.minimumTrackTintColor = [UIColor colorWithRed:71.00/255.00 green:148.00/255.00 blue:239.00/255.00 alpha:1];
    lengthSlider.maximumTrackTintColor = [UIColor colorWithRed:0.933 green:0.933 blue:0.933 alpha:1];
    [parent addSubview:lengthSlider];
    //
    widthEnabledBtn=[[UIButton alloc]initWithFrame:CGRectMake(20, 106, 24, 24)];
    [parent addSubview:widthEnabledBtn];
    [widthEnabledBtn setImage:[UIImage imageNamed:@"btn_Radio_nor.png"]  forState:UIControlStateNormal];
    [widthEnabledBtn setImage:[UIImage imageNamed:@"btn_Radio_sel.png"]   forState:UIControlStateSelected];
    [widthEnabledBtn addTarget:self action:@selector(clickWidthEnabled:) forControlEvents:UIControlEventTouchUpInside];
    
    widthTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(54, 109, 60, 18)];
    [parent addSubview:widthTitleLabel];
    widthTitleLabel.text=@"面宽";
    widthTitleLabel.font =[UIFont fontWithName:@"" size:14];
    widthTitleLabel.textColor = [UIColor colorWithHex:0x4a4a4a];;
    widthTitleLabel.textAlignment=1;
    
    widthLabel = [[UILabel alloc]initWithFrame:CGRectMake(102, 109, 80, 18)];
    [parent addSubview:widthLabel];
    widthLabel.text=@"不限";
    widthLabel.font =[UIFont fontWithName:@"" size:14];
    widthLabel.textColor = [UIColor colorWithHex:0x4a4a4a];;
    widthLabel.textAlignment=1;
    
    widthSlider = [[UISlider alloc]initWithFrame:CGRectMake(20, 146, 214, 6)];
    widthSlider.maximumValue = 12;
    widthSlider.minimumValue = 0.6;
    widthSlider.value = 3.6;
    widthSlider.minimumTrackTintColor = [UIColor colorWithRed:71.00/255.00 green:148.00/255.00 blue:239.00/255.00 alpha:1];
    widthSlider.maximumTrackTintColor = [UIColor colorWithRed:0.933 green:0.933 blue:0.933 alpha:1];
    [parent addSubview:widthSlider];
    //
    heightEnabledBtn=[[UIButton alloc]initWithFrame:CGRectMake(20, 192, 24, 24)];
    [parent addSubview:heightEnabledBtn];
    [heightEnabledBtn setImage:[UIImage imageNamed:@"btn_Radio_nor.png"]  forState:UIControlStateNormal];
    [heightEnabledBtn setImage:[UIImage imageNamed:@"btn_Radio_sel.png"]   forState:UIControlStateSelected];
    [heightEnabledBtn addTarget:self action:@selector(clickHeightEnabled:) forControlEvents:UIControlEventTouchUpInside];
    
    heightTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(54, 195, 60, 18)];
    [parent addSubview:heightTitleLabel];
    heightTitleLabel.text=@"房高";
    heightTitleLabel.font =[UIFont fontWithName:@"" size:14];
    heightTitleLabel.textColor = [UIColor colorWithHex:0x4a4a4a];
    heightTitleLabel.textAlignment=1;
    
    heightLabel = [[UILabel alloc]initWithFrame:CGRectMake(102, 195, 80, 18)];
    [parent addSubview:heightLabel];
    heightLabel.text=@"不限";
    heightLabel.font =[UIFont fontWithName:@"" size:14];
    heightLabel.textColor = [UIColor colorWithHex:0x4a4a4a];
    heightLabel.textAlignment=1;
  
    heightSlider = [[UISlider alloc]initWithFrame:CGRectMake(20, 232, 214, 6)];
    heightSlider.maximumValue = 12;
    heightSlider.minimumValue = 1.2;
    heightSlider.value = 2.7;
    heightSlider.minimumTrackTintColor = [UIColor colorWithRed:71.00/255.00 green:148.00/255.00 blue:239.00/255.00 alpha:1];
    heightSlider.maximumTrackTintColor = [UIColor colorWithRed:0.933 green:0.933 blue:0.933 alpha:1];
    [parent addSubview:heightSlider];
    //
    lengthEnabledBtn.selected = NO;
    widthEnabledBtn.selected = NO;
    heightEnabledBtn.selected = NO;
    
    lengthEnabledBtn.adjustsImageWhenHighlighted = NO;
    widthEnabledBtn.adjustsImageWhenHighlighted = NO;
    heightEnabledBtn.adjustsImageWhenHighlighted = NO;
    
    lengthSlider.enabled = NO;
    widthSlider.enabled = NO;
    heightSlider.enabled = NO;
    //
    lengthLabel.highlightedTextColor = [UIColor colorWithHex:0x4794ef];
    widthLabel.highlightedTextColor = [UIColor colorWithHex:0x4794ef];
    heightLabel.highlightedTextColor = [UIColor colorWithHex:0x4794ef];
    
    lengthLabel.textColor = [UIColor colorWithHex:0x7d8483];
    widthLabel.textColor =[UIColor colorWithHex:0x7d8483];
    heightLabel.textColor = [UIColor colorWithHex:0x7d8483];
    
    lengthTitleLabel.textColor = [UIColor colorWithHex:0x7d8483];
    widthTitleLabel.textColor = [UIColor colorWithHex:0x7d8483];
    heightTitleLabel.textColor = [UIColor colorWithHex:0x7d8483];
    
    lengthTitleLabel.highlightedTextColor = [UIColor colorWithHex:0x333333];
    widthTitleLabel.highlightedTextColor = [UIColor colorWithHex:0x333333];
    heightTitleLabel.highlightedTextColor = [UIColor colorWithHex:0x333333];
    //
    [widthSlider addTarget:self action:@selector(changeWidthValue:) forControlEvents:UIControlEventValueChanged];
    [lengthSlider addTarget:self action:@selector(changeLengthValue:) forControlEvents:UIControlEventValueChanged];
    [heightSlider addTarget:self action:@selector(changeHeightValue:) forControlEvents:UIControlEventValueChanged];
    
    if(self.sizeDic!=nil){
        [self refreshUI];
    }else{
        self.sizeDic =  [NSMutableDictionary dictionary];
        NSMutableDictionary *dic= [NSMutableDictionary dictionary];
        NSMutableDictionary *tp= [NSMutableDictionary dictionary];
//        [tp setValue:@"3.6" forKey:@"width"];
//        [tp setValue:@"2.7" forKey:@"height"];
//        [tp setValue:@"4.2" forKey:@"length"];
//        [tp setValue:@"4.2 * 3.6 * 2.7" forKey:@"classname"];
//        
        [tp setValue:@"999" forKey:@"id"];
        [dic setValue:tp forKey:@"999"];
        [self.sizeDic setValue:dic forKey:@"1000"];
    };
}

//尺寸
-(void)clickWidthEnabled:(UIButton *)btn{
    btn.selected = !btn.selected;
    widthSlider.enabled = btn.selected;
    widthTitleLabel.highlighted = btn.selected;
    widthLabel.highlighted  = btn.selected;
    if (btn.selected) {
        widthLabel.text = [NSString stringWithFormat:@"%.1f米",widthSlider.value];
        self.sizeDic[@"1000"][@"999"][@"width"] = [NSString stringWithFormat:@"%.1f",widthSlider.value];
        //
        if(heightEnabledBtn.selected&&lengthEnabledBtn.selected){
            self.sizeDic[@"1000"][@"999"][@"classname"] = [NSString stringWithFormat:@"进深%.1f * 面宽%.1f * 房高%.1f",lengthSlider.value,widthSlider.value,heightSlider.value];
            return;
        }
        if(heightEnabledBtn.selected){
            self.sizeDic[@"1000"][@"999"][@"classname"] = [NSString stringWithFormat:@"面宽%.1f * 房高%.1f",widthSlider.value,heightSlider.value];
            return;
        }
        if(lengthEnabledBtn.selected){
            self.sizeDic[@"1000"][@"999"][@"classname"] = [NSString stringWithFormat:@"进深%.1f * 面宽%.1f",lengthSlider.value,widthSlider.value];
            return;
        }
        self.sizeDic[@"1000"][@"999"][@"classname"] = [NSString stringWithFormat:@"面宽%.1f",widthSlider.value];
        //
    }else{
        widthLabel.text = @"不限";
        self.sizeDic[@"1000"][@"999"][@"width"] = nil;
        //
        if(heightEnabledBtn.selected&&lengthEnabledBtn.selected){
            self.sizeDic[@"1000"][@"999"][@"classname"] = [NSString stringWithFormat:@"进深%.1f * 房高%.1f",lengthSlider.value,heightSlider.value];
            return;
        }
        if(heightEnabledBtn.selected){
            self.sizeDic[@"1000"][@"999"][@"classname"] = [NSString stringWithFormat:@"房高%.1f",heightSlider.value];
            return;
        }
        if(lengthEnabledBtn.selected){
            self.sizeDic[@"1000"][@"999"][@"classname"] = [NSString stringWithFormat:@"进深%.1f",lengthSlider.value];
            return;
        }
        self.sizeDic[@"1000"][@"999"][@"classname"] = nil;
        //
        
    }
}
-(void)changeWidthValue:(UISlider *)slider{
    widthLabel.text = [NSString stringWithFormat:@"%.1f米",slider.value];
    self.sizeDic[@"1000"][@"999"][@"width"] = [NSString stringWithFormat:@"%.1f",widthSlider.value];
    self.sizeDic[@"1000"][@"999"][@"classname"] = [NSString stringWithFormat:@"进深%.1f * 面宽%.1f * 房高%.1f",lengthSlider.value,widthSlider.value,heightSlider.value];
}
-(void)clickHeightEnabled:(UIButton *)btn{
    btn.selected = !btn.selected;
    heightSlider.enabled = btn.selected;
    heightTitleLabel.highlighted = btn.selected;
    heightLabel.highlighted = btn.selected;
    if (btn.selected) {
        heightLabel.text = [NSString stringWithFormat:@"%.1f米",heightSlider.value];
        self.sizeDic[@"1000"][@"999"][@"height"] = [NSString stringWithFormat:@"%.1f",heightSlider.value];
        //
        if(widthEnabledBtn.selected&&lengthEnabledBtn.selected){
            self.sizeDic[@"1000"][@"999"][@"classname"] = [NSString stringWithFormat:@"进深%.1f * 面宽%.1f * 房高%.1f",lengthSlider.value,widthSlider.value,heightSlider.value];
            return;
        }
        if(widthEnabledBtn.selected){
            self.sizeDic[@"1000"][@"999"][@"classname"] = [NSString stringWithFormat:@"面宽%.1f * 房高%.1f",widthSlider.value,heightSlider.value];
            return;
        }
        if(lengthEnabledBtn.selected){
            self.sizeDic[@"1000"][@"999"][@"classname"] = [NSString stringWithFormat:@"进深%.1f * 房高%.1f",lengthSlider.value,heightSlider.value];
            return;
        }
        self.sizeDic[@"1000"][@"999"][@"classname"] = [NSString stringWithFormat:@"房高%.1f",heightSlider.value];
        //
    }else{
        heightLabel.text = @"不限";
        self.sizeDic[@"1000"][@"999"][@"height"] = nil;
        //
        if(widthEnabledBtn.selected&&lengthEnabledBtn.selected){
            self.sizeDic[@"1000"][@"999"][@"classname"] = [NSString stringWithFormat:@"进深%.1f * 面宽%.1f",lengthSlider.value,widthSlider.value];
            return;
        }
        if(widthEnabledBtn.selected){
            self.sizeDic[@"1000"][@"999"][@"classname"] = [NSString stringWithFormat:@"面宽%.1f",widthSlider.value];
            return;
        }
        if(lengthEnabledBtn.selected){
            self.sizeDic[@"1000"][@"999"][@"classname"] = [NSString stringWithFormat:@"进深%.1f",lengthSlider.value];
            return;
        }
        self.sizeDic[@"1000"][@"999"][@"classname"] = nil;
    }
}
-(void)changeHeightValue:(UISlider *)slider{
    heightLabel.text = [NSString stringWithFormat:@"%.1f米",slider.value];
    self.sizeDic[@"1000"][@"999"][@"height"] = [NSString stringWithFormat:@"%.1f",heightSlider.value];
    self.sizeDic[@"1000"][@"999"][@"classname"] = [NSString stringWithFormat:@"进深%.1f * 面宽%.1f * 房高%.1f",lengthSlider.value,widthSlider.value,heightSlider.value];
}
-(void)clicklengthEnabled:(UIButton *)btn{
    btn.selected = !btn.selected;
    lengthSlider.enabled = btn.selected;
    lengthTitleLabel.highlighted = btn.selected;
    lengthLabel.highlighted = btn.selected;
    //
    if (btn.selected) {
        lengthLabel.text = [NSString stringWithFormat:@"%.1f米",lengthSlider.value];
        self.sizeDic[@"1000"][@"999"][@"length"] = [NSString stringWithFormat:@"%.1f",lengthSlider.value];
        //
        if(widthEnabledBtn.selected&&heightEnabledBtn.selected){
            self.sizeDic[@"1000"][@"999"][@"classname"] = [NSString stringWithFormat:@"进深%.1f * 面宽%.1f * 房高%.1f",lengthSlider.value,widthSlider.value,heightSlider.value];
            return;
        }
        if(widthEnabledBtn.selected){
            self.sizeDic[@"1000"][@"999"][@"classname"] = [NSString stringWithFormat:@"面宽%.1f * 进深%.1f",widthSlider.value,lengthSlider.value];
            return;
        }
        if(heightEnabledBtn.selected){
            self.sizeDic[@"1000"][@"999"][@"classname"] = [NSString stringWithFormat:@"进深%.1f * 房高%.1f",lengthSlider.value,heightSlider.value];
            return;
        }
        self.sizeDic[@"1000"][@"999"][@"classname"] = [NSString stringWithFormat:@"进深%.1f",lengthSlider.value];
    }else{
        lengthLabel.text = @"不限";
        self.sizeDic[@"1000"][@"999"][@"length"] = nil;
        //
        if(widthEnabledBtn.selected&&heightEnabledBtn.selected){
            self.sizeDic[@"1000"][@"999"][@"classname"] = [NSString stringWithFormat:@"面宽%.1f * 房高%.1f",widthSlider.value,heightSlider.value];
            return;
        }
        if(widthEnabledBtn.selected){
            self.sizeDic[@"1000"][@"999"][@"classname"] = [NSString stringWithFormat:@"面宽%.1f",widthSlider.value];
            return;
        }
        if(heightEnabledBtn.selected){
            self.sizeDic[@"1000"][@"999"][@"classname"] = [NSString stringWithFormat:@"房高%.1f",heightSlider.value];
            return;
        }
        self.sizeDic[@"1000"][@"999"][@"classname"] =nil;
    }
    //
}
-(void)changeLengthValue:(UISlider *)slider{
    lengthLabel.text = [NSString stringWithFormat:@"%.1f米",slider.value];
    self.sizeDic[@"1000"][@"999"][@"length"] = [NSString stringWithFormat:@"%.1f",lengthSlider.value];
    self.sizeDic[@"1000"][@"999"][@"classname"] = [NSString stringWithFormat:@"进深%.1f * 面宽%.1f * 房高%.1f",lengthSlider.value,widthSlider.value,heightSlider.value];
}
-(void)refreshUI
{
    if (self.sizeDic[@"1000"][@"999"][@"width"])
    {
        widthLabel.text = [NSString stringWithFormat:@"%@米",self.sizeDic[@"1000"][@"999"][@"width"]];
        widthLabel.highlighted = YES;
        widthTitleLabel.highlighted = YES;
        widthSlider.enabled = YES;
        widthEnabledBtn.selected = YES;
        widthSlider.value = [self.sizeDic[@"1000"][@"999"][@"width"]floatValue];
    }
    else
    {
        widthLabel.text = @"不限";
        widthLabel.highlighted = NO;
        widthTitleLabel.highlighted = NO;
        widthSlider.enabled = NO;
        widthEnabledBtn.selected = NO;
        widthSlider.value = 3.6;
    }
    if (self.sizeDic[@"1000"][@"999"][@"height"])
    {
        heightLabel.text = [NSString stringWithFormat:@"%@米",self.sizeDic[@"1000"][@"999"][@"height"]];
        heightLabel.highlighted = YES;
        heightTitleLabel.highlighted = YES;
        heightSlider.enabled = YES;
        heightEnabledBtn.selected = YES;
        heightSlider.value = [self.sizeDic[@"1000"][@"999"][@"height"]floatValue];
    }
    else
    {
        heightLabel.text = @"不限";
        heightLabel.highlighted = NO;
        heightTitleLabel.highlighted = NO;
        heightSlider.enabled = NO;
        heightEnabledBtn.selected = NO;
        heightSlider.value = 2.7;
    }
    if (self.sizeDic[@"1000"][@"999"][@"length"])
    {
        lengthLabel.text = [NSString stringWithFormat:@"%@米",self.sizeDic[@"1000"][@"999"][@"length"]];
        lengthLabel.highlighted = YES;
        lengthTitleLabel.highlighted = YES;
        lengthSlider.enabled = YES;
        lengthEnabledBtn.selected = YES;
        lengthSlider.value = [self.sizeDic[@"1000"][@"999"][@"length"]floatValue];
    }
    else
    {
        lengthLabel.text = @"不限";
        lengthLabel.highlighted = NO;
        lengthTitleLabel.highlighted = NO;
        lengthSlider.enabled = NO;
        lengthEnabledBtn.selected = NO;
        lengthSlider.value = 4.2;
    }
}

-(void)addGestureRecognizer
{
    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] init];
    [swipeLeft addTarget:self action:@selector(gestureRecognizerHandle:)];
    [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
    [self.collectionView addGestureRecognizer:swipeLeft];
    
    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] init];
    [swipeRight addTarget:self action:@selector(gestureRecognizerHandle:)];
    [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
    [self.collectionView addGestureRecognizer:swipeRight];
}
-(void)gestureRecognizerHandle:(UISwipeGestureRecognizer*)sender{
    if (sender && sender.direction==UISwipeGestureRecognizerDirectionLeft){
        [self hiddenLeft:YES];
    }else{
        [self showLeft:YES];
    }
}
@end
